#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

use LWP::Simple;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

my $guid = '9de8d190-4344-415b-bde2-8213430f48c3';
#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid);
my $comicName = $comic->getName();

my $lastStripRow = $comic->getLastStrip();
my $lastStrip = 0;

# scrape information from a page
sub scrapePage {
	my $url = shift(@_) or die "$comicName: url not passed to scrapePage";
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	my $data = {};
	my @scraped;

	$data->{title} = '';
	$data->{url} = $url;

	# scrape 'strip' value
	my $stripRegex = qr/\<tr\>\<td\>\<nobr\>(\d{2})\.(\d{2})\.(\d{4}) \<b\>by/;
	@scraped = $comic->scrapeUnique($page, $stripRegex);
	my $strip = DateTime->new(month => int($scraped[0]), day => int($scraped[1]), year => int($scraped[2]))->epoch();

	if($strip <= $lastStrip) {
		#$strip = $lastStrip;
		die "$comicName: $url is out of order?";
	}
	$strip += 1000;
	$lastStrip = $strip;
	$data->{strip} = $strip;

	# scrape 'image' value
	my $imageRegex = qr/\<img.*? src="http:\/\/(www\.)?explosm\.net\/db\/files\/(.+?)"\>/;
	@scraped = $comic->scrapeUnique($page, $imageRegex);
	my $imageFilename = $scraped[1];
	if($imageFilename =~ /placehold/) {
		die "$comicName: placeholder image found...exiting script";
	}
	$data->{image} = "http://www.explosm.net/db/files/$imageFilename";

	return $data;
}


my $stripPage = get($lastStripRow->{url})
                or die "$comicName: last page url not found";
my $lastStripData = scrapePage($lastStripRow->{url}, $stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);

$lastStrip = $lastStripRow->{strip};
while($stripPage =~ /\<a rel="next" href="\/comics\/(\d+)\/"\>Next/) {
	my $url = "http://explosm.net/comics/$1/";
	$stripPage = get($url) or die "$comicName: next strip page not found";

	#print "$url\n";
	$comic->addStrip(scrapePage($url, $stripPage));
}

$comic->commit();

